﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using ChangDiTools.Tool;
using System;
namespace ChangDiTools.Demo {
    public class TextStyles {
        [CommandMethod("NewStyle")]
        public static void NewStyle() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction()) {
                //设置TrueType字体(仿宋体）
                ObjectId styleId = db.AddTextStyle("仿宋体", "simfang.ttf");
                DBText txt1 = new DBText {
                    TextString = "仿宋体",
                    TextStyleId = styleId//文字样式
                };
                //设置SHX字体(romans）
                styleId = db.AddTextStyle("罗马字体", "romans", "gbcbig");
                DBText txt2 = new DBText {
                    TextString = "罗马字体",
                    TextStyleId = styleId,//文字样式
                    Position = txt1.Position.PolarPointsl(Math.PI / 2, 5)
                };
                //设置SHX字体(romans）
                styleId = db.AddTextStyle("垂直罗马字体", "romans", "gbcbig");
                //设置文字样式的各种属性
                styleId.SetTextStyleProp(2.0, 0.67, 15 * Math.PI / 180, true, true, true, AnnotativeStates.True, true);
                DBText txt3 = new DBText {
                    TextString = "垂直罗马字体",
                    TextStyleId = styleId,
                    Position = txt1.Position.PolarPointsl(0, 20)
                };
                //设置TrueType字体(宋体）并且有加粗、倾斜效果
                styleId = db.AddTextStyle("加粗斜宋体", "宋体", true, true, 0, 0);
                DBText txt4 = new DBText {
                    TextString = "加粗斜宋体",
                    TextStyleId = styleId,
                    Position = txt2.Position.PolarPointsl(Math.PI / 2, 5)
                };
                db.AddToModelSpace(txt1, txt2, txt3, txt4);
                txt3.SetFromTextStyle();//必须将文本加入到数据库后再匹配文字样式的属性    
                tr.Commit();
            }
        }
    }
}
